package com.example.adapter;

import java.util.ArrayList;
import java.util.List;

import com.example.qzzm.R;
import com.example.vo.Act;
import com.example.window.WindowUtil;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * 主界面的列表适配器
 * 
 * @author Administrator
 *
 */
public class MyAdapter extends BaseAdapter {

	// 数据源
	private List<Act> data;// = new ArrayList<Model>();
	// context
	private Context context;
	// Universal-image的参数设置
	private DisplayImageOptions options;

	@SuppressWarnings("deprecation")
	public MyAdapter(Context context, List<Act> models) {
		this.context = context;
		if (models != null) {
			this.data = models;
		} else {
			this.data = new ArrayList<Act>();
		}
		// 设定参数
		options = new DisplayImageOptions.Builder()
		// 设置图片在下载期间显示的图片
				.showStubImage(R.drawable.app_default)
				// 设置图片Uri为空或是错误的时候显示的图片
				.showImageForEmptyUri(R.drawable.main_header)
				// 设置图片加载/解码过程中错误时候显示的图片
				.showImageOnFail(R.drawable.main_header)
				// 设置下载的图片是否缓存在内存中
				.cacheInMemory(true)
				// 设置下载的图片是否缓存在SD卡中
				.cacheOnDisc(true)
				// 设置图片以如何的编码方式显示
				.imageScaleType(ImageScaleType.NONE)
				// 设置为RGB565比起默认的ARGB_8888要节省大量的内存
				.bitmapConfig(Bitmap.Config.RGB_565)
				// 载入图片前稍做延时可以提高整体滑动的流畅度
				.delayBeforeLoading(100)
				// 显示图片时渐变
				.displayer(new FadeInBitmapDisplayer(500)).build();
	}

	@Override
	public int getCount() {
		return data.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// 保存已浏览过的item
		final ViewHolder holder;
		if (convertView == null) {
			convertView = LayoutInflater.from(context).inflate(
					R.layout.main_item, null);
			holder = new ViewHolder();
			/* 得到各个控件的对象 */
			holder.tvTitle = (TextView) convertView.findViewById(R.id.tv_title);
			holder.tvLimit = (TextView) convertView.findViewById(R.id.tv_limit);
			holder.tvPrice = (TextView) convertView.findViewById(R.id.tv_price);
			holder.tvDistance = (TextView) convertView
					.findViewById(R.id.tv_distance);
			holder.tvPeople = (TextView) convertView
					.findViewById(R.id.tv_people);
			holder.ivPicture = (ImageView) convertView
					.findViewById(R.id.iv_picture);
			// 重新设置图片的宽度和高度
			WindowUtil wUtil = new WindowUtil(context);
			android.view.ViewGroup.LayoutParams layoutParams = holder.ivPicture
					.getLayoutParams();
			layoutParams.width = wUtil.getWidth();
			layoutParams.height = wUtil.px2dp(360);
			// holder.ivPicture.setLayoutParams(layoutParams);
			// 绑定ViewHolder对象
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 填充空间内容
		holder.tvTitle.setText(data.get(position).getName());
		holder.tvLimit.setText("限制年龄:" + data.get(position).getAge_line());
		holder.tvDistance.setText(String.valueOf((int) data.get(position)
				.getJuli()) + "KM");
		holder.tvPeople.setText("已报名人数:"
				+ String.valueOf(data.get(position).getUse_num()));
		holder.tvPrice.setText(String
				.valueOf(data.get(position).getOrg_price()));
			ImageLoader.getInstance().displayImage(data.get(position).getPic(),
					holder.ivPicture, options);

		return convertView;
	}

	class ViewHolder {
		// 头部
		public TextView tvTitle;
		// 限制
		public TextView tvLimit;
		// 价格
		public TextView tvPrice;
		// 图片链接
		public ImageView ivPicture;
		// 距离
		public TextView tvDistance;
		// 报名人数
		public TextView tvPeople;
	}
}
